
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       notify_board_led.c
  * @author     baiyang
  * @date       2022-6-8
  ******************************************************************************
  */

/*----------------------------------include-----------------------------------*/
#include "notify.h"
#include "notify_board_led.h"

#include <assert.h>

#include <rtthread.h>
#include <rtdevice.h>
/*-----------------------------------macro------------------------------------*/
#if (defined(HAL_GPIO_A_LED_PIN) && defined(HAL_GPIO_B_LED_PIN) && \
     defined(HAL_GPIO_C_LED_PIN) && defined(RT_USING_PIN))

#ifndef HAL_GPIO_LED_ON
#define HAL_GPIO_LED_ON           0
#endif
#ifndef HAL_GPIO_LED_OFF
#define HAL_GPIO_LED_OFF          1
#endif

_Static_assert((HAL_GPIO_A_LED_PIN != HAL_GPIO_B_LED_PIN) &&
              (HAL_GPIO_A_LED_PIN != HAL_GPIO_C_LED_PIN) &&
              (HAL_GPIO_B_LED_PIN != HAL_GPIO_C_LED_PIN), "Duplicate LED assignments detected");
/*----------------------------------typedef-----------------------------------*/

/*---------------------------------prototype----------------------------------*/
static void boardled_ctor(board_led_t boardled);
static bool init(notify_device_t notify_dev);
static void update(notify_device_t notify_dev);
/*----------------------------------variable----------------------------------*/
static struct notify_device_ops board_notify_dev_ops = {.NotifyDeviceDestructor = NULL,
                                                        .init                   = init,
                                                        .update                 = update,
                                                        .handle_led_control     = NULL,
                                                        .handle_play_tune       = NULL,
                                                        .play_tune              = NULL,
                                                        .rgb_control            = NULL,
                                                        .rgb_set_id             = NULL};

/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
notify_device_t boardled_probe()
{
    board_led_t boardled = (board_led_t)rt_malloc(sizeof(struct board_led));

    if (boardled) {
        rt_memset(boardled, 0, sizeof(struct board_led));
        boardled_ctor(boardled);

        return (notify_device_t)boardled;
    }

    return NULL;
}

static void boardled_ctor(board_led_t boardled)
{
    boardled->_counter = 0;
    boardled->notify_dev.ops = &board_notify_dev_ops;
}

static bool init(notify_device_t notify_dev)
{
    // setup the main LEDs as outputs
    rt_pin_mode(HAL_GPIO_A_LED_PIN, PIN_MODE_OUTPUT);
    rt_pin_mode(HAL_GPIO_B_LED_PIN, PIN_MODE_OUTPUT);
    rt_pin_mode(HAL_GPIO_C_LED_PIN, PIN_MODE_OUTPUT);

    // turn all lights off
    rt_pin_write(HAL_GPIO_A_LED_PIN, HAL_GPIO_LED_OFF);
    rt_pin_write(HAL_GPIO_B_LED_PIN, HAL_GPIO_LED_OFF);
    rt_pin_write(HAL_GPIO_C_LED_PIN, HAL_GPIO_LED_OFF);
    return true;
}


/*
  main update function called at 50Hz
 */
static void update(notify_device_t notify_dev)
{
    board_led_t boardled = (board_led_t)notify_dev;

    boardled->_counter++;

    // we never want to update LEDs at a higher than 16Hz rate
    if (boardled->_counter % 3 != 0) {
        return;
    }

    // counter2 used to drop frequency down to 16hz
    uint8_t counter2 = boardled->_counter / 3;

    // initialising
    if (notify_flags.initialising) {
        // blink LEDs A and C at 8Hz (full cycle) during initialisation
        rt_pin_write(HAL_GPIO_A_LED_PIN, (counter2 & 1) ? HAL_GPIO_LED_ON : HAL_GPIO_LED_OFF);
        rt_pin_write(HAL_GPIO_C_LED_PIN, (counter2 & 1) ? HAL_GPIO_LED_OFF : HAL_GPIO_LED_ON);
        return;
	}

    // save trim and ESC calibration
    if (notify_flags.save_trim || notify_flags.esc_calibration) {
        static uint8_t save_trim_counter = 0;
        if ((counter2 & 0x2) == 0) {
            save_trim_counter++;
        }
        switch(save_trim_counter) {
            case 0:
                rt_pin_write(HAL_GPIO_C_LED_PIN, HAL_GPIO_LED_OFF);
                rt_pin_write(HAL_GPIO_A_LED_PIN, HAL_GPIO_LED_ON);
                break;

            case 1:
                rt_pin_write(HAL_GPIO_A_LED_PIN, HAL_GPIO_LED_OFF);
                rt_pin_write(HAL_GPIO_B_LED_PIN, HAL_GPIO_LED_ON);
                break;

            case 2:
                rt_pin_write(HAL_GPIO_B_LED_PIN, HAL_GPIO_LED_OFF);
                rt_pin_write(HAL_GPIO_C_LED_PIN, HAL_GPIO_LED_ON);
                break;

            default:
                save_trim_counter = -1;
                break;
        }
        return;
    }

    // arming light
    static uint8_t arm_counter = 0;
	if (notify_flags.armed) {
        // red led solid
        rt_pin_write(HAL_GPIO_A_LED_PIN, HAL_GPIO_LED_ON);
    }else{
        if ((counter2 & 0x2) == 0) {
            arm_counter++;
        }
        if (notify_flags.pre_arm_check) {
            // passed pre-arm checks so slower single flash
            switch(arm_counter) {
                case 0:
                case 1:
                case 2:
                    rt_pin_write(HAL_GPIO_A_LED_PIN, HAL_GPIO_LED_ON);
                    break;
                case 3:
                case 4:
                case 5:
                    rt_pin_write(HAL_GPIO_A_LED_PIN, HAL_GPIO_LED_OFF);
                    break;
                default:
                    // reset counter to restart the sequence
                    arm_counter = -1;
                    break;
            }
        }else{
            // failed pre-arm checks so double flash
            switch(arm_counter) {
                case 0:
                case 1:
                case 3:
                case 4:
                    rt_pin_write(HAL_GPIO_A_LED_PIN, HAL_GPIO_LED_ON);
                    break;

                case 2:
                case 5:
                case 6:
                    rt_pin_write(HAL_GPIO_A_LED_PIN, HAL_GPIO_LED_OFF);
                    break;

                default:
                    arm_counter = -1;
                    break;
            }
        }
    }

    // gps light
    switch (notify_flags.gps_status) {
        case 0:
            // no GPS attached
            rt_pin_write(HAL_GPIO_C_LED_PIN, HAL_GPIO_LED_OFF);
            break;

        case 1:
            // GPS attached but no lock, blink at 4Hz
            if ((counter2 & 0x3) == 0) {
                rt_pin_write(HAL_GPIO_C_LED_PIN, (rt_base_t)!rt_pin_read(HAL_GPIO_C_LED_PIN));
            }
            break;

        case 2:
            // GPS attached but 2D lock, blink more slowly (around 2Hz)
            if ((counter2 & 0x7) == 0) {
                rt_pin_write(HAL_GPIO_C_LED_PIN, (rt_base_t)!rt_pin_read(HAL_GPIO_C_LED_PIN));
            }
            break;

        default:
            // solid blue on gps lock
            rt_pin_write(HAL_GPIO_C_LED_PIN, HAL_GPIO_LED_ON);
            break;        
    }
}

/*------------------------------------test------------------------------------*/
#endif

